/**
 * 动态更改网站的 favicon
 * @param userId - 用户ID (1: 男性/Jun, 2: 女性/Xing)
 */
export function updateFavicon(userId: string | number) {
  // 移除所有已存在的 favicon 链接（除了 manifest）
  const existingLinks = document.querySelectorAll("link[rel='icon'], link[rel='apple-touch-icon']");
  existingLinks.forEach(link => link.remove());

  // 获取基础路径
  // @ts-ignore - Vite 会注入这个环境变量
  const base = import.meta.env?.BASE_URL || '/';
  
  // 添加时间戳防止缓存
  const timestamp = new Date().getTime();
  
  // 根据 userId 选择图标（如果需要动态切换）
  // 目前使用统一的图标文件夹
  const iconBasePath = `${base}favicon_logosc/`;
  
  // 创建多个尺寸的 favicon 链接
  const icons = [
    { rel: 'icon', type: 'image/x-icon', href: `${iconBasePath}favicon.ico?t=${timestamp}` },
    { rel: 'icon', type: 'image/png', sizes: '16x16', href: `${iconBasePath}favicon-16x16.png?t=${timestamp}` },
    { rel: 'icon', type: 'image/png', sizes: '32x32', href: `${iconBasePath}favicon-32x32.png?t=${timestamp}` },
    { rel: 'apple-touch-icon', sizes: '180x180', href: `${iconBasePath}apple-touch-icon.png?t=${timestamp}` },
  ];
  
  icons.forEach(iconConfig => {
    const link = document.createElement('link');
    link.rel = iconConfig.rel;
    link.type = iconConfig.type;
    if (iconConfig.sizes) {
      link.setAttribute('sizes', iconConfig.sizes);
    }
    link.href = iconConfig.href.replace(/([^:]\/)\/+/g, "$1");
    document.head.appendChild(link);
  });
  
  console.log(`Favicon updated for userId: ${userId}`);
}

